package cn.hrbnu.util;

import cn.hrbnu.enums.AppHttpCodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;


@Setter
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<Object> implements Serializable {
    private Integer code;
    private String msg;
    private Object data;

    public ResponseResult() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }

    public ResponseResult(Integer code, Object data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ResponseResult<?> errorResult(int code, String msg) {
        ResponseResult<Void> result = new ResponseResult<>();
        return result.error(code, msg);
    }

    public ResponseResult<Object> okResult() {
        return new ResponseResult<>();
    }

    public ResponseResult<Object> okResult(int code, String msg) {
        ResponseResult<Object> result = new ResponseResult<>();
        return result.ok(code, null, msg);
    }

    public ResponseResult<Object> okResult(Object data) {
        //指定泛型参数为Object，避免原始类型警告
        ResponseResult<Object> result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public ResponseResult<Object> errorResult(AppHttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getMsg());
    }

    public ResponseResult<Object> errorResult(AppHttpCodeEnum enums, String msg) {
        return setAppHttpCodeEnum(enums, msg);
    }

    public ResponseResult<Object> setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return okResult(enums.getCode(), enums.getMsg());
    }

    private ResponseResult<Object> setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg) {
        return okResult(enums.getCode(), msg);
    }

    public ResponseResult<?> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(Integer code, Object data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<Object> ok(Integer code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(Object data) {
        this.data = data;
        return this;
    }

}
